package edu.gmu.swe632.fruit.activity;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import edu.gmu.swe632.fruit.R;
import edu.gmu.swe632.fruit.service.GestureRecognizerService;
import edu.gmu.swe632.fruit.util.CustomAdapter;
import edu.gmu.swe632.fruit.util.RowData;
import event.GestureListener;
import event.StateEvent;

abstract public class DistanceActivity extends ListActivity implements GestureListener
{
	private static final String TAG = "distanceactivity";
	private static final String[] title = new String[] { "Confirm" };
	private static final String[] detail = new String[] { "" };
	private static final Integer[] imgid = { R.drawable.check };

	private LayoutInflater mInflater;
	private Vector<RowData> data;
	private RowData rd;
	private boolean isRecognizing = false;
	private boolean isAllowed = true;
	private Timer allowTimer = null;

	protected WheelView hundreds;
	protected WheelView tens;
	protected WheelView ones;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.distance);

		mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		data = new Vector<RowData>();

		for (int i = 0; i < title.length; i++)
		{
			try
			{
				rd = new RowData(i, title[i], detail[i]);
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
			data.add(rd);
		}

		CustomAdapter adapter = new CustomAdapter(this, mInflater, R.layout.list_item, R.id.title,
				data, imgid);
		setListAdapter(adapter);
		getListView().setTextFilterEnabled(true);

		hundreds = (WheelView) findViewById(R.id.hundreds);
		hundreds.setViewAdapter(new NumericWheelAdapter(this, 0, 9));
		hundreds.setCurrentItem(0);

		tens = (WheelView) findViewById(R.id.tens);
		tens.setViewAdapter(new NumericWheelAdapter(this, 0, 9));
		tens.setCurrentItem(0);

		ones = (WheelView) findViewById(R.id.ones);
		ones.setViewAdapter(new NumericWheelAdapter(this, 0, 9));
		ones.setCurrentItem(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.sos_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.menu_sos:
				startActivity(new Intent(this, SosActivity.class));
				break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_VOLUME_UP)
		{
			if (isAllowed)
			{
				if (isRecognizing)
				{
					Toast.makeText(this, GestureRecognizerService.CAPTURED, Toast.LENGTH_SHORT)
							.show();
					GestureRecognizerService.stopRecognizer();
				}
				else
				{
					Toast.makeText(this, GestureRecognizerService.CAPTURE, Toast.LENGTH_SHORT)
							.show();
					GestureRecognizerService.startRecognizer();
				}

				isRecognizing = !isRecognizing;
				isAllowed = false;
				Log.d(TAG, "disallow");
			}

			if (allowTimer != null)
				allowTimer.cancel();

			allowTimer = new Timer();
			allowTimer.schedule(new AllowTask(), 400);

			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private class AllowTask extends TimerTask
	{
		@Override
		public void run()
		{
			DistanceActivity.this.isAllowed = true;
			Log.d(TAG, "allow");
		}
	}

	@Override
	public void stateReceived(StateEvent event)
	{
	}
}
